﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class ProductManage_RFProductManage_Manage_ZTEPIMAndPowerManage : System.Web.UI.Page
{
    private ZTEPIMAndPower zte;

    public ProductManage_RFProductManage_Manage_ZTEPIMAndPowerManage()
    {
        zte = new ZTEPIMAndPower();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindZTEPP();
        }
    }

    private void BindZTEPP()
    {
        this.GridView1.DataSource = zte.LoadEntities();
        this.GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindZTEPP();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        zte.Delete(id);
        BindZTEPP();
    }

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        DetailsView d = (DetailsView)this.GridView1.Controls[0].Controls[0].FindControl("DetailsView1");
        ZTEPIMAndPowerInfo zteInfo = new ZTEPIMAndPowerInfo();
        zteInfo.ID = 0;
        zteInfo.ProductTypeID = ((TextBox)(d.Rows[0].Cells[1].Controls[0])).Text.Trim();
        zteInfo.TestType = ((DropDownList)(d.Rows[1].Cells[1].FindControl("DDLTestType"))).SelectedValue;
        zteInfo.TestItem = ((TextBox)(d.Rows[2].Cells[1].Controls[0])).Text.Trim();
        zteInfo.TestPort = ((TextBox)(d.Rows[3].Cells[1].Controls[0])).Text.Trim(); 
        zteInfo.TestTarget = ((TextBox)(d.Rows[4].Cells[1].Controls[0])).Text.Trim();
        zteInfo.IsPass = "Pass";

        ZTEPIMAndPowerInfo zteInfo1 = zte.GetZTEPIMandPowerbyPIDTTTI(zteInfo.ProductTypeID, zteInfo.TestType, zteInfo.TestItem);
        if (string.IsNullOrEmpty(zteInfo1.TestItem))
        {
            zte.Insert(zteInfo);
            string str1 = "添加成功！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            BindZTEPP();
        }
        else
        {
            string str1 = "加不进去了，此产品的此类型测试中已经有此项测试！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
    }

    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        BindZTEPP();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }
}
